package biz.datalk.industrialland.admin.modules.system.operator.impl;

import biz.datalk.commons.utils.StringUtils;
import biz.datalk.industrialland.admin.modules.system.mapper.ISysDictExtMapper;
import biz.datalk.industrialland.admin.modules.system.mapper.ISysDictItemExtMapper;
import biz.datalk.industrialland.admin.modules.system.operator.SysDictAndItemOperator;
import biz.datalk.industrialland.common.def.CommonConstant;
import biz.datalk.industrialland.mbg.mapper.SysDictItemMapper;
import biz.datalk.industrialland.mbg.mapper.SysDictMapper;
import biz.datalk.industrialland.mbg.pojo.po.SysDict;
import biz.datalk.industrialland.mbg.pojo.po.SysDictItem;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Priority;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统数据字典与字典条目操作器-基础实现
 * @author ZhouYi
 * @date 2020/6/11 17:4
 * @description description
 * @note note
 */
@Component("sysDictAndItemOperatorBasicImpl")
@Priority(CommonConstant.INITIAL_PRIORITY)
public class SysDictAndItemOperatorBasicImpl implements SysDictAndItemOperator {

	// ===== ===== ===== ===== [常量] ===== ===== ===== ===== //



	// ===== ===== ===== ===== [静态变量] ===== ===== ===== ===== //



	// ===== ===== ===== ===== [成员变量] ===== ===== ===== ===== //

	/** 系统数据字典定义操作映射 */
	@Resource
	private SysDictMapper sysDictMapper;

	/** 系统数据字典定义操作映射（扩展） */
	@Resource
	private ISysDictExtMapper sysDictExtMapper;

	/** 系统数据字典条目操作映射 */
	@Resource
	private SysDictItemMapper sysDictItemMapper;

	/** 系统数据字典条目操作映射（扩展） */
	@Resource
	private ISysDictItemExtMapper sysDictItemExtMapper;

	// ===== ===== ===== ===== [操作方法] ===== ===== ===== ===== //

	@Override
	public SysDict getSysDict(String dictId, String dictCode) {
		// STEP Number 声明相关变量
		SysDict sysDict = null;

		// STEP Number 使用字典ID查询
		if (StringUtils.isNotEmpty(dictId)) {
			sysDict = sysDictMapper.selectByDictId(dictId);

		}

		// STEP Number 使用字典码查询
		if ((sysDict == null) && StringUtils.isNotEmpty(dictCode)) {
			sysDict = sysDictExtMapper.selectByDictCode(dictCode);

		}

		// STEP Number 返回查询结果
		return sysDict;

	}

	@Override
	public List<SysDictItem> getSysDictItemList(String dictId, String dictCode) {
		// STEP Number 声明相关变量
		List<SysDictItem> sysDictItemList = null;

		// STEP Number 使用字典ID查询
		if (StringUtils.isNotEmpty(dictId)) {
			sysDictItemList = sysDictItemExtMapper.selectByDictId(dictId);

		}

		// STEP Number 使用字典码查询
		if (CollectionUtils.isEmpty(sysDictItemList) && StringUtils.isNotEmpty(dictCode)) {
			sysDictItemList = sysDictItemExtMapper.selectByDictCode(dictCode);

		}

		// STEP Number 返回查询结果
		return sysDictItemList;

	}

	@Override
	public Map<String, String> getSysDictItemMap(String dictId, String dictCode) {
		// STEP Number 调用相关方法，返回处理结果
		return extractSysDictItemMap(getSysDictItemList(dictId, dictCode));

	}

	@Override
	public Map<String, String> extractSysDictItemMap(List<SysDictItem> dictItemList) {
		// STEP Number 校验传入参数
		if (dictItemList == null) {
		    return null;

		}

		// STEP Number 声明相关变量
		Map<String, String> dictItemMap = new HashMap<String, String>();

		// STEP Number 循环执行处理
		for (SysDictItem item : dictItemList) {
			if (item != null) {
				String itemValue = item.getItemValue();
				String itemText = item.getItemText();
				if (itemValue != null) {
					dictItemMap.put(itemValue, itemText);

				}

			}

		}

		// STEP Number 返回处理结果
		return dictItemMap;

	}

	// ===== ===== ===== ===== [实例工具方法] ===== ===== ===== ===== //



	// ===== ===== ===== ===== [静态工具方法] ===== ===== ===== ===== //





}
